package com.example.function_interface.consumer;

import com.example.function_interface.User;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 代表了接受一个输入参数并且无返回的操作
 */
public class ConsumerTest {

    @Test
    public void myTest() throws Exception{
        List<User> userList = new ArrayList<>();
        User user1 = new User(1, "张三", 12, false);
        User user2 = new User(2, "李四", 22, false);
        User user3 = new User(3, "王五", 32, true);
        User user4 = new User(4, "郑静", 2, true);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        Consumer<User> consumer = new Consumer() {
            @Override
            public void accept(Object o) {
                User user = (User) o;
                System.out.print(user.getName());
            }
        };
        Consumer<User> comboConsumer = consumer.andThen(user -> System.out.print(":" + user.getAge() + "; "));
        for (User user : userList) {
            comboConsumer.accept(user);
        }

    }
}
